package com.bytedance.retrofit2;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import java.util.Arrays;

public final class Utils {
    static final Type[] a = new Type[0];

    static Class getRawType(Type arg4) {
        Class v0_1;
        Type v0;
        for (v0 = arg4; true; v0 = ((WildcardType) v0).getUpperBounds()[0]) {
            if (v0 == null) {
                throw new NullPointerException("type == null");
            }

            if (!(v0 instanceof Class)) {
                if ((v0 instanceof ParameterizedType)) {
                    v0 = ((ParameterizedType) v0).getRawType();
                    if (!(v0 instanceof Class)) {
                        throw new IllegalArgumentException();
                    }
                } else if ((v0 instanceof GenericArrayType)) {
                    v0 = Array.newInstance(Utils.getRawType(((GenericArrayType) v0).getGenericComponentType()), 0).getClass();
                } else if ((v0 instanceof TypeVariable)) {
                    v0 = Object.class;
                } else {
                    if (!(v0 instanceof WildcardType)) {
                        break;
                    }
                }
            }
            return ((Class) v0);
        }
        throw new IllegalArgumentException("Expected a Class, ParameterizedType, or GenericArrayType, but <" + v0 + "> is of type " + v0.getClass().getName());
    }

    static boolean a(Annotation[] arg4, Class arg5) {
        boolean v0 = false;
        int v2 = arg4.length;
        int v1 = 0;
        while (v1 < v2) {
            if (arg5.isInstance(arg4[v1])) {
                v0 = true;
            } else {
                ++v1;
                continue;
            }

            return v0;
        }

        return v0;
    }

    static byte[] a(InputStream arg4) throws IOException {
        ByteArrayOutputStream v0 = new ByteArrayOutputStream();
        if (arg4 != null) {
            byte[] v1 = new byte[4096];
            while (true) {
                int v2 = arg4.read(v1);
                if (v2 != -1) {
                    v0.write(v1, 0, v2);
                    continue;
                }

                break;
            }
        }

        return v0.toByteArray();
    }

    static int a(Object arg1) {
        int v0 = arg1 != null ? arg1.hashCode() : 0;
        return v0;
    }

    public static <T> T checkNotNull(T arg1, String arg2) {
        if (arg1 == null) {
            throw new NullPointerException(arg2);
        }
        return arg1;
    }

    static Type getParameterUpperBound(int arg4, ParameterizedType arg5) {
        Type[] v0 = arg5.getActualTypeArguments();
        if (arg4 >= 0 && arg4 < v0.length) {
            Type v0_1 = v0[arg4];
            if ((v0_1 instanceof WildcardType)) {
                v0_1 = ((WildcardType) v0_1).getUpperBounds()[0];
            }

            return v0_1;
        }

        throw new IllegalArgumentException("Index " + arg4 + " not in range [0," + v0.length + ") for " + arg5);
    }

    static Type getSupertype(Type arg1, Class arg2, Class arg3) {
        if (!arg3.isAssignableFrom(arg2)) {
            throw new IllegalArgumentException();
        }
        return Utils.a(arg1, arg2, Utils.b(arg1, arg2, arg3));
    }

    static Type a(Type type, Class<?> cls, Type type2) {
        Type type3 = type2;
        while (type3 instanceof TypeVariable) {
            type3 = type3;
            type2 = a(type, (Class) cls, (TypeVariable) type3);
            if (type2 == type3) {
                return type2;
            }
            type3 = type2;
        }
        Type componentType;
        Type a;
        if ((type3 instanceof Class) && ((Class) type3).isArray()) {
            Class cls2 = (Class) type3;
            componentType = cls2.getComponentType();
            a = a(type, cls, componentType);
            if (componentType != a) {
                return new a(a);
            }
            return cls2;
        } else if (type3 instanceof GenericArrayType) {
            GenericArrayType genericArrayType = (GenericArrayType) type3;
            componentType = genericArrayType.getGenericComponentType();
            a = a(type, cls, componentType);
            if (componentType != a) {
                return new a(a);
            }
            return genericArrayType;
        } else if (type3 instanceof ParameterizedType) {
            int i;
            ParameterizedType parameterizedType = (ParameterizedType) type3;
            componentType = parameterizedType.getOwnerType();
            Type a2 = a(type, (Class) cls, componentType);
            if (a2 != componentType) {
                i = 1;
            } else {
                i = 0;
            }
            Type[] r4 = parameterizedType.getActualTypeArguments();
            int length = r4.length;
            int i2 = i;
            Type[] r1 = r4;
            for (int i3 = 0; i3 < length; i3++) {
                Type a3 = a(type, (Class) cls, r1[i3]);
                if (a3 != r1[i3]) {
                    if (i2 == 0) {
                        r1 = (Type[]) r1.clone();
                        i2 = 1;
                    }
                    r1[i3] = a3;
                }
            }
            if (i2 != 0) {
                return new b(a2, parameterizedType.getRawType(), r1);
            }
            return parameterizedType;
        } else if (!(type3 instanceof WildcardType)) {
            return type3;
        } else {
            WildcardType wildcardType = (WildcardType) type3;
            Type[] r1 = wildcardType.getLowerBounds();
            Type[] r4 = wildcardType.getUpperBounds();
            if (r1.length == 1) {
                Type lowerBound = a(type, cls, r1[0]);
                if (lowerBound == r1[0]) {
                    return wildcardType;
                }
                return new c(new Type[]{Object.class}, new Type[]{lowerBound});
            } else if (r4.length == 1) {
                Type upperBound = a(type, cls, r4[0]);
                if (upperBound != r4[0]) {
                    return new c(new Type[]{upperBound}, Utils.a);
                }
            }
            return wildcardType;
        }
    }

    static void validateServiceInterface(Class arg2) {
        if (!arg2.isInterface()) {
            throw new IllegalArgumentException("API declarations must be interfaces.");
        }

        if (arg2.getInterfaces().length > 0) {
            throw new IllegalArgumentException("API interfaces must not extend other interfaces.");
        }
    }

    static boolean a(Type arg6, Type arg7) {
        int v4_1;
        boolean v0_1;
        Type v1 = arg7;
        Type v0 = arg6;
        while (true) {
            if (v0 == v1) {
                return true;
            } else if ((v0 instanceof Class)) {
                v0_1 = v0.equals(v1);
            } else if (!(v0 instanceof ParameterizedType)) {
                if (!(v0 instanceof GenericArrayType)) {
                    break;
                }

                if (!(v1 instanceof GenericArrayType)) {
                    return false;
                }

                v0 = ((GenericArrayType) v0).getGenericComponentType();
                v1 = ((GenericArrayType) v1).getGenericComponentType();
                continue;
            } else if (!(v1 instanceof ParameterizedType)) {
                v0_1 = false;
            } else {
                Type v4 = ((ParameterizedType) v0).getOwnerType();
                Type v5 = ((ParameterizedType) v1).getOwnerType();
                if (v4 != v5) {
                    if (v4 != null && (v4.equals(v5))) {
                        v4_1 = 1;
                    } else {
                        v4_1 = 0;
                    }
                } else {
                    label_23:
                    v4_1 = 1;
                }

                if (v4_1 != 0 && (((ParameterizedType) v0).getRawType().equals(((ParameterizedType) v1).getRawType())) && (Arrays.equals(((ParameterizedType) v0).getActualTypeArguments(), ((ParameterizedType) v1).getActualTypeArguments()))) {
                    return true;
                }

                v0_1 = false;
            }

            return v0_1;
        }

        if ((v0 instanceof WildcardType)) {
            if (!(v1 instanceof WildcardType)) {
                return false;
            }

            if ((Arrays.equals(((WildcardType) v0).getUpperBounds(), ((WildcardType) v1).getUpperBounds())) && (Arrays.equals(((WildcardType) v0).getLowerBounds(), ((WildcardType) v1).getLowerBounds()))) {
                return true;
            }

            return false;
        }

        if ((v0 instanceof TypeVariable)) {
            if (!(v1 instanceof TypeVariable)) {
                return false;
            }

            if (((TypeVariable) v0).getGenericDeclaration() == ((TypeVariable) v1).getGenericDeclaration() && (((TypeVariable) v0).getName().equals(((TypeVariable) v1).getName()))) {
                return true;
            }

            v0_1 = false;
        } else {
            return false;
        }

        return v0_1;
    }

    private static Type b(Type arg5, Class arg6, Class arg7) {
        Type v7 = null;
        int v1_1;
        Class v0 = arg6;
        Type v1 = arg5;
        while (true) {
            label_2:
            if (arg7 != v0) {
                if (arg7.isInterface()) {
                    Class[] v2 = v0.getInterfaces();
                    v1_1 = 0;
                    int v3 = v2.length;
                    while (true) {
                        label_10:
                        if (v1_1 < v3) {
                            if (v2[v1_1] == arg7) {
                                v7 = v0.getGenericInterfaces()[v1_1];
                                return v7;
                            } else if (arg7.isAssignableFrom(v2[v1_1])) {
                                arg5 = v0.getGenericInterfaces()[v1_1];
                                v0 = v2[v1_1];
                                v1 = arg5;
                                break;
                            } else {
                                ++v1_1;
                                continue;
                            }
                        }
                        break;
                    }
                }

                if (!v0.isInterface()) {
                    while (true) {
                        if (v0 == Object.class) {
                            return v7;
                        }

                        Class v1_2 = v0.getSuperclass();
                        if (v1_2 == arg7) {
                            v7 = v0.getGenericSuperclass();
                            return v7;
                        }

                        if (arg7.isAssignableFrom(v1_2)) {
                            arg5 = v0.getGenericSuperclass();
                            v0 = v1_2;
                            v1 = arg5;
                            break;
                        } else {
                            v0 = v1_2;
                        }
                    }
//                    return v1;
                } else {
                    return v7;
                }
            } else {
                return v1;
            }
        }
    }

    static String b(Type arg1) {
        String v0 = (arg1 instanceof Class) ? ((Class) arg1).getName() : arg1.toString();
        return v0;
    }

    static void c(Type arg1) {
        if (((arg1 instanceof Class)) && (((Class) arg1).isPrimitive())) {
            throw new IllegalArgumentException();
        }
    }

    static boolean hasUnresolvableType(Type arg6) {
        boolean v0_2;
        int v0_1;
        Type v0 = arg6;
        while (true) {
            if ((v0 instanceof Class)) {
                return false;
            } else if ((v0 instanceof ParameterizedType)) {
                Type[] v3 = ((ParameterizedType) v0).getActualTypeArguments();
                int v4 = v3.length;
                v0_1 = 0;
                while (v0_1 < v4) {
                    if (Utils.hasUnresolvableType(v3[v0_1])) {
                        return true;
                    }
                    ++v0_1;
                }
                return false;
            } else if ((v0 instanceof GenericArrayType)) {
                v0 = ((GenericArrayType) v0).getGenericComponentType();
                continue;
            } else {
                break;
            }
        }

        if ((v0 instanceof TypeVariable)) {
            v0_2 = true;
        } else if ((v0 instanceof WildcardType)) {
            v0_2 = true;
        } else {
            String v1 = v0 == null ? "null" : v0.getClass().getName();
            throw new IllegalArgumentException("Expected a Class, ParameterizedType, or GenericArrayType, but <" + v0 + "> is of type " + v1);
        }
        return v0_2;
    }

    static Type e(Type arg2) {
        if (!(arg2 instanceof ParameterizedType)) {
            throw new IllegalArgumentException("Call return type must be parameterized as Call<Foo> or Call<? extends Foo>");
        }

        return Utils.getParameterUpperBound(0, ((ParameterizedType) arg2));
    }

    static final class a implements GenericArrayType {
        private final Type a;

        public a(Type arg1) {
            this.a = arg1;
        }

        public final boolean equals(Object arg2) {
            boolean v0 = !(arg2 instanceof GenericArrayType) || !Utils.a(((Type) this), ((Type) arg2)) ? false : true;
            return v0;
        }

        public final Type getGenericComponentType() {
            return this.a;
        }

        public final int hashCode() {
            return this.a.hashCode();
        }

        public final String toString() {
            return Utils.b(this.a) + "[]";
        }
    }

    static final class b implements ParameterizedType {
        private final Type a;
        private final Type b;
        private final Type[] c;

        public b(Type type, Type type2, Type... typeArr) {
            int i;
            int i2 = 1;
            int i3 = 0;
            if (type2 instanceof Class) {
                if (type == null) {
                    i = 1;
                } else {
                    i = 0;
                }
                if (((Class) type2).getEnclosingClass() != null) {
                    i2 = 0;
                }
                if (i != i2) {
                    throw new IllegalArgumentException();
                }
            }
            this.a = type;
            this.b = type2;
            this.c = (Type[]) typeArr.clone();
            Type[] typeArr2 = this.c;
            i = typeArr2.length;
            while (i3 < i) {
                Type type3 = typeArr2[i3];
                if (type3 == null) {
                    throw new NullPointerException();
                }
                c(type3);
                i3++;
            }
        }

        public final boolean equals(Object arg2) {
            boolean v0 = !(arg2 instanceof ParameterizedType) || !Utils.a(((Type) this), ((Type) arg2)) ? false : true;
            return v0;
        }

        public Type[] getActualTypeArguments() {
            return (Type[]) this.c.clone();
        }

        public final Type getOwnerType() {
            return this.a;
        }

        public final Type getRawType() {
            return this.b;
        }

        public final int hashCode() {
            return Arrays.hashCode(this.c) ^ this.b.hashCode() ^ a((Object) this.a);
        }

        public final String toString() {
            String v0;
            StringBuilder v1 = new StringBuilder((this.c.length + 1) * 30);
            v1.append(Utils.b(this.b));
            if (this.c.length == 0) {
                v0 = v1.toString();
            } else {
                v1.append("<").append(Utils.b(this.c[0]));
                int v0_1;
                for (v0_1 = 1; v0_1 < this.c.length; ++v0_1) {
                    v1.append(", ").append(Utils.b(this.c[v0_1]));
                }

                v0 = v1.append(">").toString();
            }

            return v0;
        }
    }

    static final class c implements WildcardType {
        private final Type a;
        private final Type b;

        public c(Type[] arg4, Type[] arg5) {

            if (arg5.length > 1) {
                throw new IllegalArgumentException();
            }

            if (arg5.length == 1) {
                if (arg5[0] == null) {
                    throw new NullPointerException();
                } else {
                    Utils.c(arg5[0]);
                    if (arg4[0] != Object.class) {
                        throw new IllegalArgumentException();
                    } else {
                        this.b = arg5[0];
                        this.a = Object.class;
                    }
                }
            } else if (arg4[0] == null) {
                throw new NullPointerException();
            } else {
                Utils.c(arg4[0]);
                this.b = null;
                this.a = arg4[0];
            }
        }

        public final boolean equals(Object arg2) {
            boolean v0 = !(arg2 instanceof WildcardType) || !Utils.a(((Type) this), ((Type) arg2)) ? false : true;
            return v0;
        }

        public final Type[] getLowerBounds() {
            Type[] v0 = this.b != null ? new Type[]{this.b} : Utils.a;
            return v0;
        }

        public final Type[] getUpperBounds() {
            return new Type[]{this.a};
        }

        public final int hashCode() {
            int v0 = this.b != null ? this.b.hashCode() + 31 : 1;
            return v0 ^ this.a.hashCode() + 31;
        }

        public final String toString() {
            String v0;
            if (this.b != null) {
                v0 = "? super " + Utils.b(this.b);
            } else if (this.a == Object.class) {
                v0 = "?";
            } else {
                v0 = "? extends " + Utils.b(this.a);
            }

            return v0;
        }
    }
}

